package com.victualler.core.config.menu.service.impl;

import com.victualler.core.base.service.impl.BaseServiceImpl;
import com.victualler.core.common.dto.PageDto;
import com.victualler.core.config.menu.dto.MenuDto;
import com.victualler.core.config.menu.dto.request.MenuRequestDto;
import com.victualler.core.config.menu.dto.response.MenuResponseDto;
import com.victualler.core.config.menu.entity.MenuDao;
import com.victualler.core.config.menu.repository.MenuQueryRepository;
import com.victualler.core.config.menu.repository.MenuRepository;
import com.victualler.core.config.menu.service.MenuService;
import com.victualler.core.config.permission.entity.RoleInfoDao;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuDao,MenuRepository,MenuQueryRepository> implements MenuService {

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param menuRequestDto
     * @Function  根据条件入参PermissionRequestDto动态分页查询权限
     */
    public PageDto<MenuResponseDto> getByRequestDto(MenuRequestDto menuRequestDto, int from , int limit){
        return this.baseQueryRepository.getByRequestDto(menuRequestDto,from,limit);
    }

    /**
     * @author Guoshichang
     * @since 14:48 on 2018/7/6.
     * @param roleInfoList
     * @Function  获得某些角色下对应的菜单,(根据 角色对应的权限查询)
     */
    @Override
    public List<MenuDto> getMenuListByRoles(List<RoleInfoDao> roleInfoList) {
        List<Long> ids = new ArrayList<>();
        roleInfoList.forEach(item->{
            ids.add(item.getId());
        });
        return this.baseQueryRepository.getMenuListByRoles(ids);
    }

    /**
     * @author Guoshichang
     * @since 16:07 on 2018/7/6.
     * @param menuDtoListAll
     * @Function 将表结构转换为菜单树结构
     */
    public MenuDto chengeToMenuDto(List<MenuDto> menuDtoListAll) {
        List<MenuDto> menuDtoList = new ArrayList<>();
        if(menuDtoListAll!= null && menuDtoListAll.size() > 0){
            //放map
            Map<Long,MenuDto> menuDtoMap = new HashMap<>();
            menuDtoListAll.forEach(item->{
                MenuDto menuDto = new MenuDto();
                BeanUtils.copyProperties(item,menuDto);
                menuDtoMap.put(menuDto.getId(),menuDto);
            });
            //刷节点
            menuDtoMap.forEach((id,dtoItem)->{
               //顶层list
               if(dtoItem.getParentId().longValue() == 0){
                   menuDtoList.add(dtoItem);
               }else{
                   //取父节点
                   menuDtoMap.get(dtoItem.getParentId()).getChildList().add(dtoItem);
               }
            });
        }
        //排序
        this.sortMenu(menuDtoList.get(0));
        //返回
        return menuDtoList.get(0);
    }

    /**
     * @author Guoshichang
     * @since 15:00 on 2018/10/4.
     * @param menuDto 菜单树节点对象
     * @Function  按照desc节点进行排序
     */
    private void sortMenu(MenuDto menuDto) {
        if(menuDto.getChildList() !=null && menuDto.getChildList().size()>0) {
            System.out.println("排序节点:" + menuDto.getName());
            menuDto.getChildList().sort(Comparator.comparingInt(MenuDto::getMenuDesc));
            //递归排序
            for (MenuDto dto : menuDto.getChildList()) {
                sortMenu(dto);
            }
        }
    }

    /**
     * @author Guoshichang
     * @since 19:52 on 2018/8/28.
     * @param name 菜单名称
     * @return List
     * @Function  根据菜单名称模糊查询菜单
     */
    public List<MenuDao> getByName(String name) {
        return this.baseRepository.getByName("%"+name+"%");
    }

    /**
     * @author Guoshichang
     * @since 19:39 on 2018/9/15.
     * @return
     * @Function 查询所有
     */
    public List<MenuDto> getAllMenu() {
        return this.baseQueryRepository.getAll();
    }
}
